/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gac.gacmod.model;

import org.gac.gacmod.genetics.ExecutionInput;
import org.gac.gacmod.genetics.ExecutionOutput;
import org.gac.api.genetics.Population;

/**
 *
 * @author Tomek
 */
public class RepeatStep extends CompositeStep {

    private int _iterations = 0;
    
    public RepeatStep( String name )
    {
        super( name );
    }

        public RepeatStep()
    {
        this( null );
    }

    @Override
    public ExecutionOutput Run(ExecutionInput input)
    {
        if( _innerSteps.size()== 0 )
            return new ExecutionOutput(null, false, input.getPopulation());

        Population pop = input.getPopulation();
        for( int i = 0; i < _iterations; ++i)
        {
            ExecutionOutput out = _innerSteps.get(0).Run( new ExecutionInput(
                    input.getFitnessFunction(), pop));

            pop = out.getPopulation();
            if( out.isStop())
            {
                return new ExecutionOutput(out.getSolution(), true, pop);
            }
        }

        return new ExecutionOutput(null,false, pop);
    }

    @Override
    public void AddStep(Step child, int position)
    {
        if( _innerSteps.size() > 0 )
            throw new UnsupportedOperationException("This step can have only one child");
        
        super.AddStep(child, position);
    }

    /**
     * @return the _iterations
     */
    public int getIterations()
    {
        return _iterations;
    }

    /**
     * @param iterations the _iterations to set
     */
    public void setIterations(int iterations)
    {
        this._iterations = iterations;
    }


    
}
